package application.LoginAndAdministrationModule;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import application.CommunicationModule.Communication;

public class User
{

PersonData personData;
Communication connector;
Connection conn;


public User(PersonData u)
{
	this.personData = u;
	try {
		connector = new Communication("inzynier_admin","admin");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	conn = connector.getConnection();

}

public boolean change_password(Logging log, String password, String newPassword) throws Exception
{
	return log.changePassword(personData.getLogin(), password, newPassword);
}

/*
 * nameFields: name, surname, login,eMail,phoneNumber
 * Values:
 * name: String max(30)
 * surname: String max(30)
 * login: String not null, not empty max(25)
 * eMail: String max(40)
 * phoneNumber: String max(20)
 * Uwaga! Po dokonaniu edycji nale�y si� wylogowa� i zalogowa� ponownie
 * (w celu zaktualizowania danych zalogowanego u�ytkownika)
 */
public boolean edit_user_data(String nameField, String value) throws Exception
{
	String query;
	PreparedStatement pstmt = null;
	try
	{
		query = "UPDATE \"Users\" SET \""+nameField+"\" = ? WHERE id_user = ?";
	    pstmt = conn.prepareStatement(query);
	    pstmt.setString(1, value);
	    pstmt.setInt(2, this.personData.getId());
	    pstmt.execute();
	    return true;
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		pstmt.close();
	}
	
	return false;
}

/*
 * nameFields: street, houseNumber, flatNumber,city,postCode
 * Values:
 * street: String max(30)
 * houseNumber: String bez liter
 * flatNumber: String bez liter
 * city: String max(20)
 * postCode: String max(6)
 * Uwaga! Po dokonaniu edycji nale�y si� wylogowa� i zalogowa� ponownie
 */
public boolean edit_user_address(String nameField, String value) throws Exception
{
	String query;
	ResultSet result;
	PreparedStatement pstmt = null;
	
	Integer id_address;
	
	try
	{
		query = "SELECT address FROM \"Users\" WHERE id_user = ?";
	    pstmt = conn.prepareStatement(query);
	    pstmt.setInt(1, this.personData.getId());
	    result = pstmt.executeQuery();
	    result.next();
	    id_address = result.getInt(1);
	    
	    query = "UPDATE \"Addresses\" SET \""+nameField+"\" = ? WHERE id = ?";
	    pstmt = conn.prepareStatement(query);
	    if(nameField.equals("houseNumber") || nameField.equals("flatNumber"))
	    {pstmt.setInt(1, Integer.parseInt(value));}
	    else{pstmt.setString(1, value);}
	    pstmt.setInt(2, id_address);
	    pstmt.execute();
	    
	    return true;
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		pstmt.close();
	}
	
	return false;
}

public PersonData getPersonData() {
	return personData;
}

public void setPersonData(PersonData personData) {
	this.personData = personData;
}

}